package com.guli.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.commonutils.R;
import com.guli.edu.entity.Teacher;
import com.guli.edu.entity.vo.TeacherQuery;
import com.guli.edu.service.TeacherService;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author liuzg
 * @since 2021-01-29
 */
@RestController
@RequestMapping("/edu/teacher")
@Api(tags = "讲师管理")
public class EduTeacherController {

    // 注入service
    @Autowired
    private TeacherService teacherService;

    // 1、查询所有讲师表数据
    // rest风格
    @ApiOperation(value = "查询所有讲师表数据")
    @GetMapping(value = "/findAll")
    public R findAllTeacher() {
        List<Teacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }

    // 2、逻辑删除讲师
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping(value = "/remove/{id}")
    public R removeTeacher(@PathVariable String id) {
        System.out.println("id: " + id);
        boolean flag = teacherService.removeById(id);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }



    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable Long current, @PathVariable Long limit) {

        Page<Teacher> pageTeacher = new Page<>(current, limit);
        IPage<Teacher> page = teacherService.page(pageTeacher, null);
        // 获取总记录数
        long total = pageTeacher.getTotal();
        // 获取返回数据集合
        List<Teacher> records = pageTeacher.getRecords();

        //        return R.ok().data("total",total).data("rows",records);

        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);
    }


    @ApiOperation(value = "条件查询带分页")
    @PostMapping(value = "/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable Long current, @PathVariable Long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        // 创建page对象
        Page<Teacher> pageTea = new Page<>(current, limit);
        // 构造条件
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String beginTime = teacherQuery.getBeginTime();
        String endTime = teacherQuery.getEndTime();
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(beginTime)){
            wrapper.ge("gmt_create",beginTime);
        }
        if (!StringUtils.isEmpty(endTime)){
            wrapper.le("gmt_create",endTime);
        }

        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        teacherService.page(pageTea,wrapper);
        // 获取总记录数
        long total = pageTea.getTotal();
        // 获取返回数据集合
        List<Teacher> records = pageTea.getRecords();


        return R.ok().data("total",total).data("rows",records);
    }



    @ApiOperation(value = "添加讲师")
    @PostMapping(value = "/addTeacher")
    public R addTeacher(@RequestBody Teacher teacher){
        boolean save = teacherService.save(teacher);
        if (save){
            return R.ok();
        }
        return R.error();
    }



    @ApiOperation(value = "根据id查询讲师信息")
    @GetMapping(value = "/getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        Teacher teacher = teacherService.getById(id);
//        try {
//            int i= 10/0;
//        } catch (Exception e) {
//            throw new GuliException(20001,"执行了自定义异常处理...");
//        }
        return R.ok().data("teacher",teacher);
    }


    @ApiOperation(value = "修改讲师")
    @PostMapping(value = "/updateTeacher")
    public R updateTeacher(@RequestBody Teacher teacher){
        boolean flag = teacherService.updateById(teacher);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }



}

